var orderId="";

//相关业务逻辑
$(function(){
   page.bind();
   page.getPurchaseOrderData();
});

var table;
var page={//页面总变量
		bind:function(){
			
		},
		getPurchaseOrderData:function(){
			ajaxUtil.ajaxRuquest("approval/getMyApprovalPurchaseOrderData",null,function(data){
				if(data==null||data.length<=0){
					layAlert("暂无数据");
					return false;
				} 
				var html="";
				//订单采购 
					for(var i=0;i<data.length;i++){
						var status="未审核";
						var type="订单采购";
						if(data[i].status==1){
							status="已审核";
						}
						if(data[i].status==2){
							status="退回";
						}
						if(data[i].type==2){
							type="库存采购";
						}
						html+="<div class='top'><span>采购单号:"+data[i].id+"</span> 供应商：<span class='provider'>"+data[i].providerName+"</span>  &nbsp;&nbsp;采购员："+data[i].purchasePeople+
						" &nbsp; &nbsp;币种 ："+data[i].currency+" &nbsp;<span>类型："+type+"</span> &nbsp; &nbsp; <span>状态："+status+"</span> &nbsp; &nbsp;" +
								"<button value='"+data[i].id+"' class='btn btn-primary radius update'>编辑</button>  <button class='delete btn btn-primary radius' value='"+data[i].id+"'>删除</button>"+
						"</div>"+tableHtml;
						var detailList=data[i].detailList;
						if(data[i].type==1){
							for(var j=0;j<detailList.length;j++){
								var metail=detailList[j].companyMaterialTableBean;
								var feedList=detailList[j].feedDetailList;
								var length=feedList.length+1;
								var sumPurchaseCount=feedList[0].purchaseCount;
								var sumPrice=detailList[j].price*feedList[0].purchaseCount;//总价
								var sumTaxPrice=sumPrice*detailList[j].tax/100;//总含税价 
								html+="<tr>"+ 
								"<td rowspan="+length+">"+detailList[j].materialCode+"</td>"+
								"<td rowspan="+length+">"+(metail!=null?metail.materialName:"")+"</td>"+
								"<td rowspan="+length+">"+detailList[j].purchaseCount+"</td>"+
								"<td rowspan="+length+">"+detailList[j].inCount+"</td>"+
								"<td rowspan="+length+">"+(metail!=null?metail.model:"")+"</td>"+
								"<td rowspan="+length+">"+(metail!=null?metail.unit:"")+"</td>"+
								"<td rowspan="+length+">"+detailList[j].price+"</td>"+
								"<td class='tax' rowspan="+length+">"+detailList[j].tax+"%</td>"+
								"<td rowspan="+length+">"+(metail!=null?metail.defaultWarehouse:"")+"</td>"+
								"<td rowspan="+length+">"+(metail!=null?metail.materialType:"")+"</td>"+
								"<td>"+feedList[0].orderId+"</td>"+
								"<td>"+sumPurchaseCount+"</td>"+
								"<td>"+sumPrice+"</td>"+
								"<td>"+sumTaxPrice.toFixed(2)+"</td>"+
								"</tr>";
								for(var z=1;z<feedList.length;z++){
									var price=detailList[j].price*feedList[z].purchaseCount;
									sumPurchaseCount+=feedList[z].purchaseCount;
									sumPrice+=price;
									sumTaxPrice+=price*detailList[j].tax/100;
									html+="<tr>"+
									"<td>"+feedList[z].orderId+"</td>"+
									"<td>"+feedList[z].purchaseCount+"</td>"+
									"<td>"+price+"</td>"+
									"<td>"+(price*detailList[j].tax/100).toFixed(2)+"</td>"+
									"</tr>";
								}
								html+="<tr>"+
								"<td>合计</td>"+
								"<td>"+sumPurchaseCount+"</td>"+
								"<td class='sumPrice'>"+sumPrice+"</td>"+
								"<td>"+sumTaxPrice.toFixed(2)+"</td>"+
								"</tr>";
							}
						}else{
							for(var j=0;j<detailList.length;j++){
								var metail=detailList[j].companyMaterialTableBean;
								var sumPrice=detailList[j].price*detailList[j].purchaseCount;//总价
								var sumTaxPrice=sumPrice*detailList[j].tax/100;//总含税价
								html+="<tr>"+
								"<td>"+detailList[j].materialCode+"</td>"+
								"<td>"+metail!=null?metail.materialName:""+"</td>"+
								"<td>"+detailList[j].purchaseCount+"</td>"+
								"<td>"+detailList[j].inCount+"</td>"+
								"<td>"+metail!=null?metail.model:""+"</td>"+
								"<td>"+metail!=null?metail.unit:""+"</td>"+
								"<td>"+detailList[j].price+"</td>"+
								"<td class = 'tax'>"+detailList[j].tax+"%</td>"+
								"<td>"+metail!=null?metail.defaultWarehouse:""+"</td>"+
								"<td>"+metail!=null?metail.materialType:""+"</td>"+
								"<td>无</td>"+
								"<td>"+detailList[j].purchaseCount+"</td>"+
								"<td>"+sumPrice+"</td>"+
								"<td>"+sumTaxPrice.toFixed(2)+"</td>"+
								"</tr>";
							}
						} 
						html+=tableEndHtml;
				}
				$("#purchaseDiv").html(html);
				page.afterBind();
			});
		},
		afterBind:function(){
			$(".delete").unbind("click");
			$(".update").unbind("click");
			$(".delete").bind("click",function(){
				var id=$(this).val();
				ajaxUtil.ajaxRuquest("approval/deletePurchaseOrder",{"purchaseId":id},function(data){
					layer.msg(data,{icon:1});
					 page.getPurchaseOrderData();
				});
			});
			
			$(".update").bind("click",function(){
				var text = $(this).text();
				var $td = $(this).parent().next().find("table").eq(0).find("tr td.tax");
				if(text=="编辑"){
					for(var i=0;i<$td.length;i++){
						var taxVal = $td.eq(i).text();
						$td.eq(i).html("<input class = 'input-text' type='text' value="+taxVal+"></input>");
					}
					$(this).text("修改");
					
				}
				if(text=="修改"){
					var id = $(this).val();
					var purchaseArray = new Array();
					var $tr = $(this).parent().next().find("table").eq(0).find("tr").filter(function(){
						return $(this).find("td.tax").length>0;
					});
					for(var i=0;i<$tr.length;i++){
						var purchaseDetail = new Object();
						var materialCode = $tr.eq(i).find("td").eq(0).text();
						var tax = $tr.eq(i).find("td.tax input").val().trim();  
						if(tax===''||tax===null){
							layAlert("税率不能为空");
							return false;
						} 
						purchaseDetail.materialCode = materialCode;
						purchaseDetail.mainId = id;
						purchaseDetail.tax = parseInt(tax);
						purchaseArray.push(purchaseDetail);
					} 
					ajaxUtil.ajaxRuquest("approval/updatePurchaseOrder",{"purchaseArray":JSON.stringify(purchaseArray)},function(data){
	                	 layer.msg(data,{icon:1});
	                	 page.getPurchaseOrderData();
					});
					
				}
			});
			
//			$(".notAgree").bind("click",function(){
//				var id=$(this).val();
//                ajaxUtil.ajaxRuquest("approval/setPurchaseStatus",{"id":id,"status":2},function(data){
//                	 layer.msg(data,{icon:1});
//                	 page.getPurchaseOrderData();
//				});
//			});
		}
}


var tableHtml="<div class='center'>"+
		             "<table class='table table-border table-bordered table-bg table-hover'>"+
		                  "<thead>"+
		                     " <tr>"+
		                     "<th>物料代码</th>"+
	                         "<th>物料名称</th>"+
	                         "<th>采购总数</th>"+
	                         "<th>入库数量</th>"+
	                         "<th>规格型号</th>"+
	                         "<th>单位</th>"+
	                         "<th>含税单价</th>"+
	                         "<th>税率</th>"+
	                         "<th>仓库</th>"+
	                         "<th>物料类型</th>"+
	                         "<th>关联单号</th>"+
	                         "<th>采购数量</th>"+
	                         "<th>含税金额</th>"+
	                         "<th>税额</th>"+
		                      "</tr>"+
		                 "</thead>"+
		                 "<tbody>";
var tableEndHtml="</tbody></table><div>";
